import {defineStore} from 'pinia'
import {nextTick} from "vue";
import {getSystemCommonConfig} from "@/api/system/SystemConfigService";


/**
 *  应用仓库接口
 *
 * @author 徐寿春
 * 2023/5/18 11:06
 */
interface CumAppSate {
    config: Record<string, any>
    // 是否是移动设备
    isMobile: boolean
    // 是否折叠
    isCollapsed: boolean
    // 路由是否显示
    isRouteShow: boolean
}

export const useAppStore = defineStore('app', {

    state: (): CumAppSate => ({
        config: {},
        isMobile: true,
        isCollapsed: false,
        isRouteShow: true
    }),


    actions: {
        getImageUrl(url: string) {
            return url ? `${this.config.ossDomain}${url}` : ''
        },


        setMobile(value: boolean) {
            this.isMobile = value
        },


        /**
         * 重新配置配置信息调用刷新
         */
        getConfig() {
            return new Promise((resolve, reject) => {
                getSystemCommonConfig()
                    .then((data: any) => {
                        this.config = data
                        resolve(data)
                    })
                    .catch((err: any) => {
                        reject(err)
                    })
            })
        },

        /**
         * 切换侧边栏
         *
         * @param toggle
         */
        toggleCollapsed(toggle?: boolean) {
            this.isCollapsed = toggle ?? !this.isCollapsed
        },


        /**
         * 刷新视图
         */
        refreshView() {
            this.isRouteShow = false
            nextTick(() => {
                this.isRouteShow = true
            }).then(r => {
            })
        }
    }
})

export default useAppStore
